<template>
  <div class="admin-login-page">
    <div class="login-container">
      <div class="login-card">
        <div class="login-header">
          <h1>Easy Go 后台管理系统</h1>
          <p>管理员登录入口</p>
        </div>
        
        <el-form 
          :model="form" 
          :rules="rules" 
          ref="loginForm" 
          class="login-form"
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              placeholder="管理员账号"
              size="large"
              :prefix-icon="User"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              type="password"
              placeholder="管理员密码"
              size="large"
              :prefix-icon="Lock"
              show-password
            />
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              size="large" 
              class="login-btn"
              :loading="loading"
              @click="handleLogin"
            >
              {{ loading ? '登录中...' : '管理员登录' }}
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="login-footer">
          <p>测试账号: admin / 123456</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useTokenStore } from '@/stores/token'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const tokenStore = useTokenStore()
const userStore = useUserStore()
const loginForm = ref()

const loading = ref(false)

const form = reactive({
  username: '',
  password: ''
})

const rules = reactive({
  username: [
    { required: true, message: '请输入管理员账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ]
})

const handleLogin = async () => {
  // 表单验证
  if (!await loginForm.value.validate()) return
  
  loading.value = true
  
  try {
    console.log('管理员登录请求:', form.username)
    
    // 调用登录API - 暂时使用模拟登录
    // 实际项目中这里应该调用真实的后端API
    const mockLogin = () => {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve({
            success: true,
            token: 'mock_admin_token_' + Date.now(),
            user: {
              id: 1,
              username: form.username,
              role: 'admin',
              loginTime: new Date().toISOString()
            }
          })
        }, 1000)
      })
    }
    
    const data = await mockLogin()
    
    if (data.success) {
      // 登录成功
      tokenStore.setToken(data.token)
      userStore.setUser(data.user)
      
      ElMessage.success('管理员登录成功！')
      console.log('管理员登录成功:', data.user)
      
      // 跳转到后台首页
      router.push('/')
    } else {
      ElMessage.error('登录失败: ' + (data.message || '未知错误'))
    }
    
  } catch (error) {
    console.error('登录错误:', error)
    ElMessage.error('网络错误，请稍后重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.admin-login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 400px;
}

.login-card {
  background: white;
  padding: 2.5rem;
  border-radius: 12px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 2rem;
}

.login-header h1 {
  color: #2c3e50;
  margin-bottom: 0.5rem;
  font-size: 1.5rem;
  font-weight: 600;
}

.login-header p {
  color: #7f8c8d;
  margin: 0;
  font-size: 0.9rem;
}

.login-form {
  margin-bottom: 1.5rem;
}

.login-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
}

.login-footer {
  text-align: center;
  padding-top: 1.5rem;
  border-top: 1px solid #ecf0f1;
}

.login-footer p {
  color: #95a5a6;
  font-size: 0.8rem;
  margin: 0;
}
</style>